package com.yyy.instagram.Adaper;

import android.animation.ValueAnimator;
import android.os.Build;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.rebound.SimpleSpringListener;
import com.facebook.rebound.Spring;
import com.facebook.rebound.SpringConfig;
import com.facebook.rebound.SpringSystem;
import com.timehop.stickyheadersrecyclerview.StickyRecyclerHeadersAdapter;
import com.yyy.instagram.R;

/**
 * Created by qqqqqqq on 2016/9/29.
 */

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements StickyRecyclerHeadersAdapter {
    private int count = 0;
    private long firClick;
    private long secClick;
    private Spring spring;
    private Spring springBig;
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_tweet_content, parent, false);
        SpringSystem springSystem = SpringSystem.create();

        spring = springSystem.createSpring();
        spring.setSpringConfig(SpringConfig.fromOrigamiTensionAndFriction(60,5));
        springBig = springSystem.createSpring();
        springBig.setSpringConfig(SpringConfig.fromOrigamiTensionAndFriction(60,5));
        return new TweetViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        if(holder instanceof TweetViewHolder) {
            final TweetViewHolder tvh = (TweetViewHolder) holder;
            tvh.image.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    if (isDoubleClick(v, event)) {
                        showBigLike(tvh);
                        clickLike(tvh, true);
                    }
                    return true;
                }
            });
            tvh.like.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    clickLike(tvh, !tvh.like.isSelected());
                }
            });
        }

    }

    public boolean isDoubleClick(View v, MotionEvent event){
        if(MotionEvent.ACTION_DOWN == event.getAction()){
            count++;
            if(count == 1){
                firClick = System.currentTimeMillis();
            } else if (count == 2){
                secClick = System.currentTimeMillis();
                if(secClick - firClick < 500){

                    count = 0;
                    firClick = 0;
                    secClick = 0;
                    return true;
                }else{
                    count = 1;
                    firClick = secClick;
                    secClick = 0;
                }
            }
        }
        return false;
    }

    public void showBigLike(final TweetViewHolder holder){
        holder.likeBig.setVisibility(View.VISIBLE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            holder.likeBig.setImageAlpha(255);
        }else{
            holder.likeBig.setAlpha(100);
        }
        spring.addListener(new SimpleSpringListener() {

            @Override
            public void onSpringUpdate(Spring spring) {
                // You can observe the updates in the spring
                // state by asking its current value in onSpringUpdate.
                float value = (float) spring.getCurrentValue();
                holder.likeBig.setScaleX(value);
                holder.likeBig.setScaleY(value);
            }

            @Override
            public void onSpringAtRest(Spring spring) {
                super.onSpringAtRest(spring);
                hideBigLike(holder);
                spring.removeAllListeners();

            }
        });
        spring.setCurrentValue(0);
        spring.setEndValue(1);
    }
    public void hideBigLike(final TweetViewHolder holder){
        ValueAnimator animator = ValueAnimator.ofInt(255,0);
        animator.setInterpolator(new LinearInterpolator());
        animator.setDuration(200);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int value = (int) animation.getAnimatedValue();
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                    holder.likeBig.setImageAlpha(value);
                }else{
                    holder.likeBig.setAlpha(value);
                }
            }
        });
        animator.start();
    }

    public void clickLike(TweetViewHolder holder,Boolean select){
        holder.like.setImageResource(select ? R.drawable.direct_heart:R.drawable.insights_like);
        holder.like.setSelected(select);
        clickLikeAnimator(holder);
    }
    public void clickLikeAnimator(final TweetViewHolder holder){
        springBig.addListener(new SimpleSpringListener() {

            @Override
            public void onSpringUpdate(Spring spring) {
                // You can observe the updates in the spring
                // state by asking its current value in onSpringUpdate.
                float value = (float) spring.getCurrentValue();
                holder.like.setScaleX(value);
                holder.like.setScaleY(value);
            }

            @Override
            public void onSpringAtRest(Spring spring) {
                super.onSpringAtRest(spring);
                springBig.removeAllListeners();
            }
        });
        springBig.setCurrentValue(0);
        springBig.setEndValue(1);
    }


    @Override
    public long getHeaderId(int position) {
        return position;
    }


    @Override
    public RecyclerView.ViewHolder onCreateHeaderViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_tweet_header, parent, false);
        return new TweetHeaderViewHolder(view);
}

    @Override
    public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder, int position) {

    }

    @Override
    public int getItemCount() {
        return 10;
    }


    class TweetViewHolder extends RecyclerView.ViewHolder{
        public SimpleDraweeView image;
        public ImageView like;
        public ImageView likeBig;
        public TweetViewHolder(View itemView) {
            super(itemView);
            image = (SimpleDraweeView) itemView.findViewById(R.id.tweet_image);
            like = (ImageView) itemView.findViewById(R.id.tweet_like);
            likeBig = (ImageView) itemView.findViewById(R.id.tweet_like_big);
        }

    }
    class TweetHeaderViewHolder extends  RecyclerView.ViewHolder{
        public  TweetHeaderViewHolder(View itemView){
            super(itemView);
        }
    }
}
